package it.polimi.TravelDream.ejb.searchManager;

import it.polimi.TravelDream.ejb.dtos.ExcursionDTO;
import it.polimi.TravelDream.ejb.entities.Excursion;
import it.polimi.TravelDream.ejb.entities.Group;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class ExcursionManager
 */
@Stateless
@LocalBean
public class ExcursionManager implements ExcursionMgr {

	@PersistenceContext
    private EntityManager em;
	private String name;
	private String nation;
	private List <Excursion> result = new ArrayList <Excursion>();
	
    /**
     * Default constructor. 
     */
    public ExcursionManager() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public List <ExcursionDTO> getResultList(){
    	
    	List <ExcursionDTO> ExcursionDTOs = new ArrayList <ExcursionDTO>();
    	
    	for (Excursion e:this.result){
    		ExcursionDTO excursionDTO = convertToDTO(e);
    		ExcursionDTOs.add(excursionDTO);
    	}
    	return ExcursionDTOs;
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public void search(){
    	this.result = em.createNamedQuery(Excursion.FIND_EXCURSION_BY_NAME_AND_NATION, Excursion.class).setParameter("name", this.name).setParameter("nation",this.nation).getResultList();
    	
    }
    
    @Override
    @RolesAllowed({Group._ADMIN})
    public String getName() {
		return name;
	}

    @Override
    @RolesAllowed({Group._ADMIN})
	public void setName(String name) {
		this.name = name;
	}

    @Override
    @RolesAllowed({Group._ADMIN})
	public String getNation() {
		return nation;
	}

    @Override
    @RolesAllowed({Group._ADMIN})
	public void setNation(String nation) {
		this.nation = nation;
	}

	private ExcursionDTO convertToDTO (Excursion excursion){
    	ExcursionDTO excursionDTO = new ExcursionDTO();
    	excursionDTO.setName(excursion.getName());
    	excursionDTO.setDescription(excursion.getDescription());
    	excursionDTO.setNation(excursion.getNation());
    	excursionDTO.setPlacesAvailable(excursion.getPlacesAvailable());
    	excursionDTO.setPrice(excursion.getPrice());
    	
    	return excursionDTO;
    }
	
	@Override
	@RolesAllowed({Group._ADMIN})
	public void searchAll() {
		this.result = em.createNamedQuery(Excursion.FIND_ALL_EXCURSIONS, Excursion.class).getResultList();
	}
  
	@Override
	@RolesAllowed({Group._ADMIN})
	public void update(ExcursionDTO excursionDTO, int index) {
		Excursion excursion = new Excursion();
		
		excursion = copyValuesFromDTO(excursionDTO, index);
		
		em.merge(excursion);
	}
	
	private Excursion copyValuesFromDTO(ExcursionDTO excursionDTO, int index) {
		Excursion excursion = new Excursion();
		
		excursion = result.get(index);
		
		excursion.setName(excursionDTO.getName());
    	excursion.setDescription(excursionDTO.getDescription());
    	excursion.setNation(excursionDTO.getNation());
    	excursion.setPlacesAvailable(excursionDTO.getPlacesAvailable());
    	excursion.setPrice(excursionDTO.getPrice());
    	
    	return excursion;
		
	}

}
